<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;

/**
 * 活动相关操作
 * @author demontf
 *
 */
//允许跨域访问
header('Access-Control-Allow-Origin: *');

class ActivityController extends Controller {

	public function __call($method, $args){
		if (method_exists('ActivityController', $method)){
			$this->$method;
		}else{
			$response['STATUS'] = "error";
			$response['ERRMSG'] = "无该方法";
			$response['RESULT'] = array();
			$this->ajaxReturn($response);
		}
	}
    
    /**
     * 获取活动
     * @param aid  活动id 
     * @param phone 
     * 调用地址：/sign/index.php/Home/Activity/get
     * @return {"STATUS":"error|success","ERRMSG":"参数错误|超时|发送失败","RESULT":[]}
     */
    public function get(){
    		$aid =  I('param.aid/i',0);
    		$phone = I('param.phone/s');
    		if(empty($aid)){
    			$response['STATUS'] = "error";
    			$response['ERRMSG'] = "参数错误";
    			$response['RESULT'] = array("aid"=>$aid);
    			$this->ajaxReturn($response);
    		}
    		$M = new Model();
    		$res=$M->field("t.name as t_name,t.pic as t_pic,t.color,t.description as t_des,t.academy,t.school_name
    				  ,a.name as title,a.description as a_des,a.status,a.background as bg,a.time,a.term,a.other1,a.other2,a.other3")
    		  ->table("activity as a,teacher as t")
    		  ->where("t.id = a.teacher and a.id=$aid")
    		  ->select();
    		$isSign = 0;
    		if(!empty($phone)){
    			$M= new Model();
    			$isSign=$M->field('r.id,r.term')->table('record as r, user as u ')->where("u.phone=$phone and u.id = r.user")->select();
    		}
    		$times = 1;
    		if(!empty($phone)){
    			$M= new Model();
    			$times=$M->table('record as r, user as u ')->where("u.phone=$phone and u.id = r.user")->count();
    		}
    		
    		if($res){
    			$response['STATUS'] = "success";
    			$response['ERRMSG'] = "";
    			$response['RESULT'] = array("data"=>$res,"isSign"=>$isSign,"times"=>$times);
    			$this->ajaxReturn($response);
    		}else{
    			$response['STATUS'] = "error";
    			$response['ERRMSG'] = "活动不存在";
    			$response['RESULT'] = array();
    			$this->ajaxReturn($response);
    		}
    		
    }
    

   
}
